<!-- eslint-disable no-undef -->
<script setup lang="ts">
import { reqGetLogisticsDetail } from '@/services/order'
import type { Logistics } from '@/types/order'
import AMapLoader from '@amap/amap-jsapi-loader'
import startIcon from '@/assets/start.png'
import endIcon from '@/assets/end.png'
import carIcon from '@/assets/car.png'
const route = useRoute()
const logistics = ref<Logistics>()
// 获取物流详情
const getLogisTicsDetail = async () => {
  const res = await reqGetLogisticsDetail(route.params.id as string)
  console.log(res)
  logistics.value = res.data
}
getLogisTicsDetail()
// ---------------------------------------------
// 1. 配置秘钥
window._AMapSecurityConfig = {
  securityJsCode: '8f63c7d8273ae29766aed13a58548db2'
}
// 2.配置完秘钥后 引入地图 配置key初始化地图
onMounted(() => {
  AMapLoader.load({
    key: '70651b1c37522442860192ef9f9a204f', // 申请好的Web端开发者Key，首次调用 load 时必填
    version: '2.0' // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
  })
    .then((AMap) => {
      const map = new AMap.Map('map', {
        //设置地图容器id
        viewMode: '3D', //是否为3D地图模式
        zoom: 20, //初始化地图级别
        mapStyle: 'amap://styles/whitesmoke' // 设置地图样式
      })
      // 路线规划
      AMap.plugin('AMap.Driving', function () {
        var driving = new AMap.Driving({
          // 驾车路线规划策略，AMap.DrivingPolicy.LEAST_TIME是最快捷模式
          // policy: AMap.DrivingPolicy.LEAST_TIME
          map,
          showTraffic: false, // 不显示交通
          hideMarkers: true // 隐藏标记
        })
        // 如果使用[0] [length - 1]方式固然能取到开始的经纬度和结束的经纬度, 但是待会再做 途径站点的经纬度
        const start = logistics.value?.logisticsInfo.shift() // 取出起始的经纬度
        const end = logistics.value?.logisticsInfo.pop() // 取出终点的经纬度
        var startLngLat = [start?.longitude, start?.latitude]
        // 自定义起点覆盖物
        var startMarker = new AMap.Marker({
          icon: startIcon,
          position: startLngLat,
          anchor: 'center'
        })
        map.add(startMarker) // 追加覆盖物
        var endLngLat = [end?.longitude, end?.latitude]
        var endMarker = new AMap.Marker({
          icon: endIcon,
          position: endLngLat,
          anchor: 'center'
        })
        map.add(endMarker) // 追加覆盖物

        driving.search(
          startLngLat,
          endLngLat,
          {
            waypoints: logistics.value?.logisticsInfo.map((item) => [
              item.longitude,
              item.latitude
            ])
          },
          function (status: any, result: any) {
            // 未出错时，result即是对应的路线规划方案
            var carMarker = new AMap.Marker({
              icon: carIcon,
              position: [
                logistics.value?.currentLocationInfo.longitude,
                logistics.value?.currentLocationInfo.latitude
              ],
              anchor: 'center'
            })
            map.add(carMarker) // 追加覆盖物
            setTimeout(() => {
              map.setFitView([carMarker])
              map.setZoom(10)
            }, 3000)
          }
        )
      })
    })
    .catch((e) => {
      console.log(e)
    })
})
</script>

<template>
  <div class="order-logistics-page" v-if="logistics">
    <div id="map">
      <div class="title">
        <van-icon name="arrow-left" @click="$router.back()" />
        <span>{{ logistics.statusValue }}</span>
        <van-icon name="service" />
      </div>
      <div class="current">
        <p class="status">预计送达时间{{ logistics.estimatedTime }}</p>
        <p class="predict">
          <span>{{ logistics.name }}</span>
          <span>{{ logistics.awbNo }}</span>
        </p>
      </div>
    </div>
    <div class="logistics">
      <p class="title">物流详情</p>
      <van-steps direction="vertical" :active="0">
        <van-step v-for="item in logistics.list" :key="item.id">
          <p class="status">{{ item.statusValue }}</p>
          <p class="content">
            {{ item.content }}
          </p>
          <p class="time">{{ item.createTime }}</p>
        </van-step>
      </van-steps>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.order-logistics-page {
  --van-step-icon-size: 18px;
  --van-step-circle-size: 10px;
}
#map {
  height: 450px;
  background-color: var(--cp-bg);
  overflow: hidden;
  position: relative;
  .title {
    background-color: #fff;
    height: 46px;
    width: 355px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    padding: 0 15px;
    font-size: 16px;
    position: absolute;
    left: 10px;
    top: 10px;
    box-sizing: border-box;
    box-shadow: 0px 0px 22px 0px rgba(229, 229, 229, 0.5);
    z-index: 999;
    > span {
      flex: 1;
      text-align: center;
    }
    .van-icon {
      font-size: 18px;
      color: #666;
      &:last-child {
        color: var(--cp-primary);
      }
    }
  }
  .current {
    height: 80px;
    border-radius: 4px;
    background-color: #fff;
    height: 80px;
    width: 355px;
    box-sizing: border-box;
    padding: 15px;
    position: absolute;
    left: 10px;
    bottom: 10px;
    box-shadow: 0px 0px 22px 0px rgba(229, 229, 229, 0.5);
    z-index: 999;
    .status {
      font-size: 15px;
      line-height: 26px;
    }
    .predict {
      color: var(--cp-tip);
      font-size: 13px;
      margin-top: 5px;
      > span {
        padding-right: 10px;
      }
    }
  }
}
.logistics {
  padding: 0 10px 20px;
  .title {
    font-size: 16px;
    padding: 15px 5px 5px;
  }
  .van-steps {
    :deep(.van-step) {
      &::after {
        display: none;
      }
    }
    .status {
      font-size: 15px;
      color: var(--cp-text3);
      margin-bottom: 4px;
    }
    .content {
      font-size: 13px;
      color: var(--cp-tip);
      margin-bottom: 4px;
    }
    .time {
      font-size: 13px;
      color: var(--cp-tag);
    }
  }
}
</style>
